package com.an.common.utils.ip;

import cn.hutool.core.net.NetUtil;
import cn.hutool.http.HtmlUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.an.common.core.domain.address.BaiduAddressResult;
import com.an.common.enums.BaiduAddressRespCodeType;
import com.an.common.exception.ServiceException;
import com.an.common.properties.BaiduIpProperties;
import com.an.common.utils.ServletUtils;
import com.an.common.utils.StringUtils;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * 获取地址类
 *
 * @author Lion Li
 */
@Slf4j
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class AddressUtils {


    /**
     * 未知
     */
    public static final String UNKNOWN = "XX XX";


    /**
     * 调用百度接口，获取带有经纬度的地址信息
     *
     * @param baiduIpProperties 百度IP属性
     * @return {@link BaiduAddressResult }
     * @author: Ann
     * @createDate: 2023/11/02 17:29:38
     * @version: 1.0.0
     */
    public static BaiduAddressResult getAddressInfoWithLatAndLon(BaiduIpProperties baiduIpProperties) {
        //百度ip地点查询的请求地址
        String url = baiduIpProperties.getUrl();
        //key
        String ak = baiduIpProperties.getAk();
        String clientIp = ServletUtils.getClientIP();
        Boolean innerIp = isInnerIp(clientIp);
        log.warn("客户端IP:{}",clientIp);
        Map<String, Object> paramsMap = new HashMap<>(3);
        paramsMap.put("coor" , "bd09ll");
        paramsMap.put("ak" , ak);
        if (!innerIp) {
            paramsMap.put("ip" , clientIp);
        }
        BaiduAddressResult baiduAddressResult;
        try {
            String responseBody = HttpUtil.get(url, paramsMap);
            baiduAddressResult = JSONUtil.toBean(responseBody, BaiduAddressResult.class);
            if (BaiduAddressRespCodeType.NORMAL.getCode() != baiduAddressResult.getStatus()) {
                throw new ServiceException(BaiduAddressRespCodeType.getMsgByCode(baiduAddressResult.getStatus()));
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new ServiceException(e.getMessage());
        }
        return baiduAddressResult;
    }


    /**
     * 获得真正ip地址
     *
     * @param ip 知识产权
     * @return {@link String }
     * @author: Ann
     * @createDate: 2023/09/26 15:03:56
     * @version: 1.0.0
     */
    public static String getRealAddressByIp(String ip) {
        if (StringUtils.isBlank(ip)) {
            return UNKNOWN;
        }
        Boolean innerIp = isInnerIp(ip);
        if (innerIp) {
            return "内网IP";
        }
        return RegionUtils.getCityInfo(ip);
    }

    /**
     * 是内部IP
     *
     * @param ip 知识产权
     * @return {@link Boolean }
     * @author: Ann
     * @createDate: 2023/11/21 14:46:24
     * @version: 1.0.0
     */
    public static Boolean isInnerIp(String ip) {
        // 内网不查询
        ip = StringUtils.equalsAny(ip, "0:0:0:0:0:0:0:1" , "::1" , "::ffff:127.0.0.1") ? "127.0.0.1" : HtmlUtil.cleanHtmlTag(ip);
        return NetUtil.isInnerIP(ip);
    }


    /**
     * 通过IP获取真正省份
     *
     * @param ip 知识产权
     * @return {@link String }
     * @author: Ann
     * @createDate: 2023/09/26 15:04:58
     * @version: 1.0.0
     */
    public static String getRealProvinceByIp(String ip) {
        String address = getRealAddressByIp(ip);
        if ("内网IP".equals(address)) {
            return address;
        }
        //从完整的ip属地提取省份  中国|河南省|郑州市|移动
        String[] addressArray = address.split("\\|");
        String province = addressArray[1];
        if (province.contains("省")) {
            province = province.replace("省" , "");
        }
        return province;
    }

}
